#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btnClear_clicked()
{
    ui->plainTextEdit->clear();
}


void Widget::on_btnFrontBack_clicked()
{
    ui->plainTextEdit->appendPlainText("front(), back()函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    ui->plainTextEdit->appendPlainText("front=" + QString(str1.front()));
    ui->plainTextEdit->appendPlainText("back=" + QString(str1.back()));
}


void Widget::on_btnLeftRight_clicked()
{
    ui->plainTextEdit->appendPlainText("right(), left()函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    // QString::right(n) get right n QChars
    ui->plainTextEdit->appendPlainText("left=" + QString(str1.right(9)));
    // QString::left(n) get left n QChars
    ui->plainTextEdit->appendPlainText("right=" + QString(str1.left(2)));
}


void Widget::on_btnFirstLast_clicked()
{
    ui->plainTextEdit->appendPlainText("first(), last()函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    QString str2 = ui->comboBoxStr2->currentText();

    // str2从右到左切割str1一次, 第一部分作为first, 第二部分作为last, 形如文件路径、文件名
    ui->plainTextEdit->appendPlainText("str1:" + str1);
    ui->plainTextEdit->appendPlainText("str2:" + str2);
    int pos = str1.lastIndexOf(str2);
    QString path = str1.first(pos + 1);
    QString name = str1.last(str1.size() - pos - 1);

    ui->plainTextEdit->appendPlainText("路径名称：" + path);
    ui->plainTextEdit->appendPlainText("文件名称：" + name);
}


void Widget::on_btnSection_clicked()
{
    ui->plainTextEdit->appendPlainText("section函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    QString str2 = ui->comboBoxStr2->currentText();
    int n = ui->spinBox->value();
    QString strSection = str1.section(str1, n, n);

    ui->plainTextEdit->appendPlainText(strSection);
}


void Widget::on_btnIsNullIsEmpty_clicked()
{
    ui->plainTextEdit->appendPlainText("isNull, isEmpty函数测试");
    QString str1, str2 = "";
    QString str;
    ui->plainTextEdit->appendPlainText("QString str1, str2=\"\"");


    // test str1
    str = "str1.isNull() = ";
    if (str1.isNull())
        str += "true";
    else
        str += "false";
    ui->plainTextEdit->appendHtml(str);

    str = "str1.isEmpty() = ";
    if (str1.isEmpty())
        str += "true";
    else
        str += "false";
    ui->plainTextEdit->appendPlainText(str);


    // test str2
    str = "str2.isNull() = ";
    if (str2.isNull())
        str += "true";
    else
        str += "false";
    ui->plainTextEdit->appendHtml(str);

    str = "str2.isEmpty() = ";
    if (str2.isEmpty())
        str += "true";
    else
        str += "false";
    ui->plainTextEdit->appendPlainText(str);
}


void Widget::on_btnSizeLength_clicked()
{
    ui->plainTextEdit->appendPlainText("size, count, length函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    ui->plainTextEdit->appendPlainText(QString::asprintf("size = %d", str1.size()));
    ui->plainTextEdit->appendPlainText(QString::asprintf("count = %d", str1.count()));
    ui->plainTextEdit->appendPlainText(QString::asprintf("length = %d", str1.length()));
}


void Widget::on_btnFill_clicked()
{
    ui->plainTextEdit->appendPlainText("fill函数测试");
    QString str = "Hello";
    str.fill('X');    // XXXXX
    ui->plainTextEdit->appendPlainText(str);
    str.fill('A', 2); // AA
    ui->plainTextEdit->appendPlainText(str);
    str.fill(QChar(0x54C8), 3); // 哈哈哈
    ui->plainTextEdit->appendPlainText(str);
}


void Widget::on_btnResize_clicked()
{
    ui->plainTextEdit->appendPlainText("resize函数测试");
    QString str = "Hello";
    ui->plainTextEdit->appendPlainText(QString::asprintf("str size = %d", str.size()));
    str.resize(3);
    ui->plainTextEdit->appendPlainText(QString::asprintf("str size = %d", str.size()));
}


void Widget::on_btnTrimmedSimplified_clicked()
{
    ui->plainTextEdit->appendPlainText("trimmed, simplified函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    QString str2 = str1.trimmed();
    ui->plainTextEdit->appendPlainText("trimmed() = " + str2);

    str2 = str1.simplified();
    ui->plainTextEdit->appendPlainText("simplified() = " + str2);
}


void Widget::on_btnInsert_clicked()
{
    ui->plainTextEdit->appendPlainText("insert函数测试");
    QString str = "It is great";
    ui->plainTextEdit->appendPlainText(str);

    int n = str.lastIndexOf(" ");
    str.insert(n, "n't");
    ui->plainTextEdit->appendPlainText(str);
}


void Widget::on_btnRemove_clicked()
{
    // 移除特定的字符
    ui->plainTextEdit->appendPlainText("remove移除特定的字符");
    QString str1 = "你们，我们，他们";
    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    QString delStr = "们";
    QChar delChar = QChar(delStr[0].unicode());
    str1.remove(delChar);
    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    // 移除右侧N个字符
    ui->plainTextEdit->appendPlainText("remove右侧N个字符");
    str1 = "G:\\Qt6Book\\QtSamples\\qw.cpp";
    ui->plainTextEdit->appendPlainText(str1);

    int n = str1.lastIndexOf("\\");
    str1.remove(n + 1, 20);
    ui->plainTextEdit->appendPlainText(str1);
}


void Widget::on_btnReplace_clicked()
{
    // 替换指定的字符
    ui->plainTextEdit->appendPlainText("replace, 替换指定的字符");
    QString str1 = "Goooogle";
    ui->plainTextEdit->appendPlainText(str1);

    str1.replace('o', 'e');
    ui->plainTextEdit->appendPlainText(str1);

    // 替换指定范围的字符串
    ui->plainTextEdit->appendPlainText("replace, 替换指定范围字符串");
    str1 = "It is great";
    ui->plainTextEdit->appendPlainText(str1);

    int n = str1.lastIndexOf(" ");
    QString subStr = "wonderful";
    str1.replace(n + 1, subStr.size(), subStr);
    ui->plainTextEdit->appendPlainText(str1);

    str1.replace(n + 1, subStr.size(), "OK!");
    qDebug() << str1;                      // "It is OK!"
    qDebug() << str1.toLocal8Bit().data(); // It is OK!
    ui->plainTextEdit->appendPlainText(str1);
}

